package cn.biplam.front.component;

import cn.biplam.common.client.Paging;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.KvMap;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.front.html.base.HtmlWindow;

/**
 * 组件客户端响应基础类
 * @author 	WangSong
 * @create 	2009-4-19 下午06:46:52
 * @version 1.0
 */
public abstract class AjxResponse {
	protected KvMap<String, String> params=null;
	protected UserInfo mUser;
	protected String sHomeUrl;
	protected String actionId;
	protected String recordId;
	protected String containerId;
	protected String targetId;
	/**是否强制脱壳 */
	protected boolean forceEnvelopeOut = false;

	/**
	 * 向Ajx客户端请求返回创建好的页面对象
	 * @throws ErrorMessage
	 */
	protected abstract HtmlWindow ajxGet() throws ErrorMessage;

	/**
	 * 响应客户端脚本通讯,返回包含客户端绑定脚本的结果文本
	 * @param actionId	组件交换代码
	 * @param params	页面参数容器(KvMap<String,String>)
	 * @param sHomeUrl	主页地址
	 * @param mUser		用户信息类
	 * @return	String
	 * @throws ErrorMessage
	 */
	public String get(String actionId,KvMap<String, String> params,String sHomeUrl,UserInfo mUser) throws ErrorMessage{
		HtmlWindow html = this.getHtml(actionId, params, sHomeUrl, mUser);
		
		StringBuilder sb = new StringBuilder();
		sb.append("<ajxt");
		sb.append(this.targetId);
		sb.append("ajxt>");

		//检查是否为空
		if(html!=null){
			/**处理绑定脚本*/
			StringBuilder sbTemp = new StringBuilder();
			html.getBind(sbTemp);
			if(sbTemp.length()>0){
				sb.append("<ajxb");
				sb.append(sbTemp);
				sb.append("ajxb>");
			}

			sbTemp.delete(0, sbTemp.length());

			/**处理html*/
			/**如果目标容器和源容器不一致,则返回完整Html*/
			if(this.containerId.equals(this.targetId) || this.forceEnvelopeOut){
				html.getContent(sbTemp);
			}else{
				html.getHtml(sbTemp);
			}
			if(sbTemp.length()>0){
				sb.append("<ajxh");
				sb.append(sbTemp);
			}
		}else{
			sb.append("<ajxb alert('脚本页面请求返回为空(");
			sb.append(actionId);
			sb.append(")!');");
			sb.append("ajxb>");
		}

		return sb.toString();
	}
	
	/**
	 * 相应客户端服务请求,返回HtmlWindow对象<BR>
	 * 	其他组件请求HtmlWindow对象可调用本接口(如果实现类不是交换类，可以忽略actionId)
	 * @param actionId	组件交换代码
	 * @param params	页面参数容器(KvMap<String,String>)
	 * @param sHomeUrl	主页地址
	 * @param mUser		用户信息类
	 * @return	String
	 * @throws ErrorMessage
	 */
	public HtmlWindow getHtml(String actionId,KvMap<String, String> params,String sHomeUrl,UserInfo mUser) throws ErrorMessage{
		this.actionId=actionId;
		this.params=params;
		this.sHomeUrl=sHomeUrl;
		this.mUser=mUser;
		this.recordId=this.params.getByKey(".rid");
		this.params.removeByKey(".rid");
		this.containerId=this.params.getByKey(".cid");
		this.params.removeByKey(".cid");
		this.targetId=this.params.getByKey(".tid");
		this.params.removeByKey(".tid");
		setPageNum();
		return this.ajxGet();
	}

	public void setSHomeUrl(String homeUrl) {
		sHomeUrl = homeUrl;
	}

	public void setContainerId(String containerId) {
		this.containerId = containerId;
	}

	/**
	 * 设置分页信息
	 * @throws ErrorMessage 
	 */
	private void setPageNum() throws ErrorMessage{
		if(params!=null && params.getByKey(containerId+"optIdPage")!=null){
			Paging.setPersist(mUser.getUserID(), params.getByKey(containerId+"optIdPage"), TOOLS.stringToInt(params.getByKey(containerId+"pageNum")));
		}
	}
}